package hu.bme.mit.mdsd.csipetcsapat.appstore.core;

import java.util.Map;
import java.util.HashMap;

/*
 * RoleDispatcher class for handling roles
 */
public class RoleDispatcher {
	private static boolean isInitialized;
	private static Map<RoleName, Role> roleMap = new HashMap<RoleName, Role>();
	private static Role administrator;
	private static Role registereduser;
	private static Role developer;
	private static Role enduser;
	private static Role user;
	public enum RoleName {
		ADMINISTRATOR, REGISTEREDUSER, DEVELOPER, ENDUSER, USER
	}

	/*
 	 * Initializing the dispatcher
	 */
	private static synchronized void init() {
		if (isInitialized)
			return;
	
	administrator = new Role("ADMINISTRATOR");
	administrator.addService("DeterminePublication");
	administrator.addService("ListPendingtransactions");
	administrator.addService("RouteTransaction");
	administrator.addService("CashoutMoney");
	roleMap.put(RoleName.valueOf("ADMINISTRATOR"), administrator);
	
	registereduser = new Role("REGISTEREDUSER");
	registereduser.addService("ModifyUserdata");
	registereduser.addService("ViewCredit");
	roleMap.put(RoleName.valueOf("REGISTEREDUSER"), registereduser);
	
	developer = new Role("DEVELOPER");
	developer.addService("ListOwnapplications");
	developer.addService("UpdateApplication");
	developer.addService("UpdateMetadata");
	developer.addService("UploadApplication");
	developer.addService("CashoutMoney");
	roleMap.put(RoleName.valueOf("DEVELOPER"), developer);
	
	enduser = new Role("ENDUSER");
	enduser.addService("ListPurchasedapplications");
	enduser.addService("PurchaseApplication");
	enduser.addService("TransferMoney");
	roleMap.put(RoleName.valueOf("ENDUSER"), enduser);
	
	user = new Role("USER");
	user.addService("RegisterUser");
	user.addService("ListAllapplications");
	user.addService("SearchApplication");
	user.addService("ListApplicationbycategory");
	user.addService("LoginUser");
	roleMap.put(RoleName.valueOf("USER"), user);
	
	
	isInitialized = true;
	}

	/*
 	 * Get role name 
	 */
	public static Role getRole(RoleName roleName) {
		init();
		return roleMap.get(roleName);
	}
}
